#import "AuthenticationController.h"
#import "IDominoAppDelegate.h"

@implementation AuthenticationController
@synthesize 	userNameInput, passwordInput, serverPicker, serverPickerArray, mailPickerArray, finalPath;
static NSString* domainAddress=@"www.wiiings.com";

- (void)loadView {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
	
	UIView *contentView = [[UIView alloc] initWithFrame:screenRect];
	contentView.autoresizesSubviews = YES;
	contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	
	self.view = contentView;
	[contentView release];
	
	
	serverPickerArray = [[NSArray arrayWithObjects:
						   @"rbapns01",@"syd-nts1",nil] retain];
	mailPickerArray = [[NSArray arrayWithObjects:
						@"mail", @"au/mail", @"hk/mail",@"in/mail", nil] retain];
	UILabel* URLLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 30.0)];
	URLLabel.text = NSLocalizedString(@"AuthenServerURL", @"Server URL :");
	finalPath=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 30.0, 320.0, 30.0)];
	finalPath.text=domainAddress;
	finalPath.lineBreakMode=UILineBreakModeHeadTruncation;
	UILabel* userNameLabel=[[UILabel alloc] initWithFrame:CGRectMake(12.0, 70.0, 90.0, 30.0)];
	userNameLabel.text=NSLocalizedString(@"Username", @"Username:");
	UILabel* passwordLabel=[[UILabel alloc] initWithFrame:CGRectMake(12.0, 130.0, 90.0, 30.0)];
	passwordLabel.text=NSLocalizedString(@"Password", @"Password:");
	
	userNameInput=[[UITextField alloc] initWithFrame:CGRectMake(110.0, 70.0, 180.0, 30.0)];
	userNameInput.borderStyle=UITextBorderStyleRoundedRect;
	userNameInput.delegate=self;
	passwordInput=[[UITextField alloc] initWithFrame:CGRectMake(110.0, 130.0, 180.0, 30.0)];
	passwordInput.borderStyle=UITextBorderStyleRoundedRect;
	passwordInput.delegate=self;
	serverPicker=[[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 180.0, 320.0, 350.0)];
	serverPicker.delegate=self;
	serverPicker.dataSource=self;
	serverPicker.showsSelectionIndicator=YES;
	[self.view addSubview:URLLabel];
	[self.view addSubview:userNameLabel];
	[self.view addSubview:passwordLabel];
	[self.view addSubview:userNameInput];
	[self.view addSubview:passwordInput];
	[self.view addSubview:serverPicker];
	[self.view addSubview:finalPath];
	[userNameLabel release];
	[passwordLabel release];
	[URLLabel release];
	
	IDominoAppDelegate* appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
	userNameInput.text=appDelegate._loginUsername;
	passwordInput.text=appDelegate._loginPassword;
	[pool release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
	[finalPath release];
	[serverPickerArray release];
	[mailPickerArray release];
	[userNameInput release];
	[passwordInput release];
	[serverPicker release];
    [super dealloc];
}





- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	return 2;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	// report the selection to the UI label
	NSString* server=[serverPickerArray objectAtIndex:[pickerView selectedRowInComponent:0]];
	NSString* mail=[mailPickerArray objectAtIndex:[pickerView selectedRowInComponent:1]];
	finalPath.text=[NSString stringWithFormat:@"http://%@/%@/%@/%@.nsf", domainAddress, server,mail,userNameInput.text];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	NSString *returnStr=nil;
	if (component == 0)
	{
		returnStr = [serverPickerArray objectAtIndex:row];
	}
	else if(component == 1)
	{
		returnStr = [mailPickerArray objectAtIndex:row];
	}
	return returnStr;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
	CGFloat componentWidth=150;
	return componentWidth;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
	return 40.0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	if (component == 0)
	{
		return [serverPickerArray count];
	}
	else 
	{
		return [mailPickerArray count];	
	}

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	[textField resignFirstResponder];
	NSString* server=[serverPickerArray objectAtIndex:[serverPicker selectedRowInComponent:0]];
	NSString* mail=[mailPickerArray objectAtIndex:[serverPicker selectedRowInComponent:1]];
	finalPath.text=[NSString stringWithFormat:@"http://%@/%@/%@/%@.nsf", domainAddress,server,mail,userNameInput.text];
	return YES;
}

- (BOOL) setParameters
{
	//NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	IDominoAppDelegate* appDelegate = (IDominoAppDelegate *)[[UIApplication sharedApplication] delegate];
	appDelegate._mailServer=[NSString stringWithFormat:@"%@/%@", [serverPickerArray objectAtIndex:[serverPicker selectedRowInComponent:0]],
							 [mailPickerArray objectAtIndex:[serverPicker selectedRowInComponent:1]]];
	NSString* userName=[userNameInput text];
	NSString* password=[passwordInput text];
	if((userName==nil) || (password==nil) || ([userName isEqualToString:@""]) || ([password isEqualToString:@""]))
		return NO;
	appDelegate._loginUsername=userName;
	appDelegate._loginPassword=password;
	//[pool release];
	return YES;
}
@end
